package com.wzy.asyn;

import lombok.SneakyThrows;

import java.sql.Timestamp;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author wzy
 * @Date: 2022/3/31 11:50
 * @Description: JDK5  Future:
 * Future模式在 JDK5 的时候就有, Future模式，只是发起了耗时操作，函数立马就返回了，真正执行具体操作由另外一个工作线程去完成，
 * 并不会阻塞客户端线程。所以在工作线程执行耗时操作的时候客户端无需等待，可以继续做其他事情，等到需要的时候再向工作线程获取结果。
 */
public class CompleteThreadTest {
    public static void main(String[] args) throws Exception {
        acton();

    }

    private static void acton() throws InterruptedException {
        long start = System.currentTimeMillis();
        CompletableFuture<NormalThreadTest.KitchenWare> kitchenWare = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2500); // 挑选厨具
                System.out.println("第一步: 网上下单");
                System.out.println("第一步: 等待厨具");
                Thread.sleep(3000); // 等待厨具时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第一步: 快递送货 --- 厨具");
            return new NormalThreadTest.KitchenWare();
        });
        CompletableFuture<NormalThreadTest.FoodMaterial> foodMaterial = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("第二步: 食材已经到位");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第一步: 快递送货---食材");
            return new NormalThreadTest.FoodMaterial();
        });
        kitchenWare.thenAcceptBoth(foodMaterial, (k, f) -> {
            try {
                cooking(k, f);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long endTime = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(10);
        System.out.println("烹饪美食时间为:" + (endTime - start));

    }


    /**
     * 厨具类
     *
     * @author Administrator
     */
    static class KitchenWare {
    }

    /**
     * 食材类
     *
     * @author Administrator
     */
    static class FoodMaterial {

    }

    /**
     * 定义烹饪食物的方法
     *
     * @param kc
     * @param fm
     */
    static void cooking(NormalThreadTest.KitchenWare kc, NormalThreadTest.FoodMaterial fm) throws InterruptedException {
        TimeUnit.SECONDS.sleep(2);
        System.out.println("做饭完成");

    }
}
